//整个网页部加载完毕后再执行的
$(function () {

    function getCode(captchaBtn) {
        let email = $("#exampleInputEmail1").val()

        $.ajax({
            url: "/email/code",
            type: "GET",
            data: {
                email
            },
            success: (res) => {
                console.log(res)
                contdown(captchaBtn)

            }

        })
    }

    $("#captcha-btn").on("click", function () {
        getCode(this)
        // console.log(this)
    })

    function contdown(captchaBtn) {
        let time = 5;
        let timer = setInterval(() => {
            time -= 1;
            if (time > 0) {
                $(captchaBtn).text(time + "秒后再试");
                $(captchaBtn).attr("disabled", true);
            } else {
                clearInterval(timer);
                $(captchaBtn).text("发送验证码");
                $(captchaBtn).attr("disabled", false);
            }
        }, 1000);
    }

    $("#form").on("submit", function (e) {
        e.preventDefault()
        let formData = $('#form').serialize();
        console.dir(formData)
        $.ajax({
            url:"/register",
            type:"POST",
            data:formData,
            success:(res)=>{
                alert(res.message)
                location.href = res.redirect_url
            }

        })
    })


});

